#pragma once
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <functional>


using func = std::function<void (void )>;
static int n = 1;
//2.0版本
class Thread
{
public:
    static void *run(void *data)
    {
        Thread *temp = (Thread *)data;
        temp->_func();
        return nullptr;
    }

    enum status
    {
        WAIT,
        RUNNING,
        EXIT
    };
    void setFunc(func F)
    {
        _func=F;
    }

    
    Thread() : _name("pthread" + std::to_string(++n)), _n(n), _statu(WAIT)
    {}

    bool start()
    {
        int getr = pthread_create(&_tid, nullptr, run, (void *)this);
        // run((void*)_data.c_str());
        if (getr == -1)
            return false;
        _statu = RUNNING;
        return true;
    }

    bool isRunning()
    {
        return _statu == RUNNING;
    }

    bool exit()
    {
        int get = pthread_cancel(_tid);
        if (get == -1)
            return false;
        _statu = EXIT;
        return true;
    }
    pthread_t tid()
    {
        return _tid;
    }
    void wait()
    {
        pthread_join(_tid,nullptr);
    }
    std::string name()
    {
        return _name;
    }

private:
    std::string _name;
    pthread_t _tid;
    func _func;
    status _statu;
    int _n;
};



